package demo.utils;

import org.apache.log4j.Logger;

/**
 * 系统日志工具类 提供Logger等同的功能
 * 
 */
public class SysLogUtil {

	private static Logger log = Logger.getLogger("SYS");

	public static void print(String str) {
		log.error(str);
	}
	
	public static void info(String message) {
		if (log.isInfoEnabled()) {
			log.info(concat(message));
		}
	}

	/**
	 * 连接提示信息
	 * 
	 * @param message
	 * @return
	 */
	protected static String concat(Object message) {
		// 取调用该类的类信息
		String callStack = getCallStack();

		StringBuffer str = new StringBuffer();
		str.append("[");
		str.append(message);
		str.append("]");
		str.append("[" + callStack + "]");
		return str.toString();
	}

	/**
	 * 获取调用SysLogUtils类的调用堆栈
	 * 
	 * @return
	 */
	private static String getCallStack() {

		StringBuffer str = new StringBuffer();
		StackTraceElement stack[] = (new Throwable()).getStackTrace();
		StackTraceElement callStackEle = stack[3];
		str.append("[").append(callStackEle.getClassName()).append("]");
		str.append("[").append(callStackEle.getMethodName()).append("]");
		str.append("[行号：").append(callStackEle.getLineNumber()).append("]");
		return str.toString();
	}

}
